﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

/// <summary>
/// 输出信息
/// </summary>
internal class COutputMessage
{
    delegate void ShowMessage(RichTextBox rtb, string outmsg);

    /// <summary>
    /// 某控件中显示信息
    /// </summary>
    /// <param name="rtb"></param>
    /// <param name="outmsg"></param>
    public static void Show(RichTextBox rtb, string outmsg)
    {
        if (rtb.InvokeRequired) // 如果主线程正在使用rtb
        {
            ShowMessage show = new ShowMessage(Showing);    // 实例化委托
            rtb.Invoke(show, new object[] { rtb, outmsg });
        }
        else
        {
            Showing(rtb, outmsg);
        }
    }

    /// <summary>
    /// 执行信息显示
    /// </summary>
    /// <param name="rtb"></param>
    /// <param name="outmsg"></param>
    private static void Showing(RichTextBox rtb, string outmsg)
    {
        rtb.Text = outmsg + "(" + DateTime.Now.ToString() + ")\r\n" + rtb.Text;
    }

    delegate void ClearMessage(RichTextBox rtb);

    public static void Clear(RichTextBox rtb)
    {
        if (rtb.InvokeRequired)
        {
            ClearMessage clear = new ClearMessage(Clearing);
            rtb.Invoke(clear, new object[] { rtb }); 
        }
        else
        {
            Clearing(rtb);
        }
    }

    /// <summary>
    /// 执行清除操作
    /// </summary>
    /// <param name="rtb"></param>
    private static void Clearing(RichTextBox rtb)
    {
        rtb.Clear();
    }
}
